package com.wust.tieshi;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.wust.R;
import com.wust.baby.BabyInfo;
import com.wust.baby.BabyInfoDao;
import com.wust.menu.SecondGroupTab;
import com.wust.util.SqltimeToTime;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

public class Third extends Activity {
	private BabyInfoDao infoDao;
	private List<BabyInfo>infolist;
	private String baby_id;
	private SqltimeToTime timeTransfer;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.third);
		LinearLayout layout=(LinearLayout) findViewById(R.id.thirdlayout);
		infoDao=new BabyInfoDao(this);
		timeTransfer=new SqltimeToTime();
		SharedPreferences preferences = getSharedPreferences("userinfo",0);
		baby_id=preferences.getString("baby_id", "");
		layout.addView(execute());
	}
	 @Override  
	    public void onBackPressed() {   
	        //把后退事件交给子Activity处理  
			Intent intent = new Intent(Third.this, ViewTestActivity.class);  
	        // 把Activity转换成一个Window，然后转换成View  
	        Window w = SecondGroupTab.group.getLocalActivityManager()  
	                .startActivity("ViewTestActivity",intent);  
	        View view = w.getDecorView();  
	        //设置要跳转的Activity显示为本ActivityGroup的内容  
	        SecondGroupTab.group.setContentView(view);  
	       
	    } 
	protected XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles) {
	    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
	    setRenderer(renderer, colors, styles);
	   
	    return renderer;
	  }

	  protected void setRenderer(XYMultipleSeriesRenderer renderer, int[] colors, PointStyle[] styles) {
	    renderer.setAxisTitleTextSize(16);
	    renderer.setChartTitleTextSize(20);
	  
	    renderer.setLabelsTextSize(15);
	    renderer.setLegendTextSize(15);
	   // renderer.setBackgroundColor(Color.parseColor("#00000000"));
	    renderer.setBackgroundColor(Color.parseColor("#FFF0F5"));
	    renderer.setApplyBackgroundColor(true);
	    renderer.setPointSize(5f);
	    renderer.setMarginsColor(Color.WHITE);
	    //renderer.setMargins(new int[] { 0, 0, 0, 0 });
	    int length = colors.length;
	    for (int i = 0; i < length; i++) {
	      XYSeriesRenderer r = new XYSeriesRenderer();
	      r.setColor(colors[i]);
	      r.setPointStyle(styles[i]);
	      renderer.addSeriesRenderer(r);
	    }
	  }
	  protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
		      String yTitle, double xMin, double xMax, double yMin, double yMax, int axesColor,
		      int labelsColor) {
		    renderer.setChartTitle(title);
		    renderer.setChartTitleTextSize(27);
		    renderer.setXTitle(xTitle);
		    renderer.setYTitle(yTitle);
		    renderer.setXAxisMin(xMin);
		    renderer.setXAxisMax(xMax);
		    renderer.setYAxisMin(yMin);
		    renderer.setYAxisMax(yMax);
		    renderer.setAxesColor(axesColor);
		    renderer.setZoomButtonsVisible(true);
		    renderer.setLabelsColor(labelsColor);
		  }
	  public void addXYSeries(XYMultipleSeriesDataset dataset, String[] titles, List<double[]> xValues,
		      List<double[]> yValues, int scale) {
		    int length = titles.length;
		    for (int i = 0; i < length; i++) {
		      XYSeries series = new XYSeries(titles[i], scale);
		      double[] xV = xValues.get(i);
		      double[] yV = yValues.get(i);
		      int seriesLength = xV.length;
		      for (int k = 0; k < seriesLength; k++) {
		        series.add(xV[k], yV[k]);
		      }
		      dataset.addSeries(series);
		    }
		  }
	  protected XYMultipleSeriesDataset buildDateDataset(String titles, List<Date> xValues,
		      List<Double> yValues) {
		    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		   
		    TimeSeries series = new TimeSeries(titles);
		    for (int i = 0; i < xValues.size(); i++)   
		        series.add(xValues.get(i), yValues.get(i));
		      
		      dataset.addSeries(series);
		    
		    return dataset;
		  }
public View execute()
{
	  String titles = new String( "体重走向图" );
	    List<Date> dates = new ArrayList<Date>();
	    List<Double> values = new ArrayList<Double>();
	    infolist=infoDao.getBabyInfoListById(baby_id);
	    for (Iterator iterator = infolist.iterator(); iterator.hasNext();) {
			BabyInfo info = (BabyInfo) iterator.next();
			Date date=timeTransfer.stringToDate(info.getInfo_date(), "yyyy-MM-dd");
			dates.add(date);
			values.add(info.getInfo_weigth());
		}
	    int length=dates.size();
	   
	    int[] colors = new int[] { Color.BLUE };
	    PointStyle[] styles = new PointStyle[] { PointStyle.POINT };
	    XYMultipleSeriesRenderer renderer = buildRenderer(colors, styles);
	    setChartSettings(renderer, "体重趋势", "Date", "kg", dates.get(0).getTime(),
	        dates.get(length-1).getTime(), 2, 30, Color.BLACK, Color.BLACK);
	    renderer.setXLabels(5);
	    renderer.setYLabels(10);
	    renderer.setZoomButtonsVisible(true);
	    length = renderer.getSeriesRendererCount();
	    for (int i = 0; i < length; i++) {
	      SimpleSeriesRenderer seriesRenderer = renderer.getSeriesRendererAt(i);
	      seriesRenderer.setDisplayChartValues(true);
	    }
	    return ChartFactory.getTimeChartView(Third.this, buildDateDataset(titles, dates, values), renderer, "MM/dd/yyyy");
	    
}
}
